package com.tma.basicfragment;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class CategoryFragment extends ListFragment {
    /**
     * The container Activity must implement this interface so the fragment can
     * deliver messages
     * 
     */
    public interface OnCategorySelectedListener {
        void onCategorySelected(int position);
    }

    static final String TAG = "CategoryFragment";
    private OnCategorySelectedListener mCallback;
    private boolean mDualFragments = false;
    private int mCurrentPosition;

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("position", mCurrentPosition);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception.
        try {
            mCallback = (OnCategorySelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnCategorySelectedListener");
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getInt("position");
        }

        // We need to use a different list item layout for devices older than Honeycomb
        int layout = 
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
                        android.R.layout.simple_list_item_activated_1 : 
                        android.R.layout.simple_list_item_1;

        // Create an array adapter for the list view
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.category, layout);
        setListAdapter(adapter);

        // When in two-pane layout, set the listview to highlight the selected list item
        // (We do this during onStart because at the point the listview is available.)
        mDualFragments = false;
        if (getFragmentManager().findFragmentById(R.id.f_category_detail) != null) {
            mDualFragments = true;
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            selectPosition(mCurrentPosition);
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        mCurrentPosition = position;
        // Notify the parent activity of selected item
        mCallback.onCategorySelected(position);

        // Set the item as checked to be highlighted when in two-pane layout
        getListView().setItemChecked(position, true);
    }

    public void selectPosition(int position) {
        // Only if we're showing both fragments should the item be "highlighted"
        if (mDualFragments) {
            ListView lv = getListView();
            lv.setItemChecked(position, true);
        }
        mCallback.onCategorySelected(position);
    }
}
